package com.example;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.example.emum.GitBranchType;
import com.example.utils.GitUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Date;

//弹窗的窗口
class MyDialog extends JDialog {
    private static MyDialog instance = null;

    static JTextField addrText;
    static JTextField usernameText;
    static JTextField passwordText;
    static JTextField fileDirectoryText;
    static JTextField commitMessageText;
    static JCheckBox flagBox;

    static String gitUrl;
    static String username;
    static String password;
    static String localDir;


    public MyDialog() {
    }

    public MyDialog(SwingArea frame) {
        super(frame);
    }

    public static MyDialog getInstance(SwingArea frame) {
        if (null == instance) {
            synchronized (MyDialog.class) {
                if (null == instance) {
                    instance = new MyDialog(frame);
                }
            }
        }
        return instance;
    }

    public void initUI() {
        this.setVisible(true);
        this.setSize(400,300);
        this.setTitle("上传到git");
        this.setModal(true);
        this.setLocationRelativeTo(this.getParent());

        this.setLayout(new BorderLayout());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
                SwingArea.uploadAction.setEnabled(true);
            }
        });

        initLeft();
        initRight();
        initUnder();
    }

    private void initLeft(){
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new GridLayout(5,1));
        this.add(leftPanel, BorderLayout.WEST);

        JPanel addrPanel = new JPanel();
        JLabel addrTitle = new JLabel("gitUrl");
        addrPanel.add(addrTitle);
        leftPanel.add(addrPanel);

        JPanel usernamePanel = new JPanel();
        JLabel usernameTitle = new JLabel("username");
        usernamePanel.add(usernameTitle);
        leftPanel.add(usernamePanel);

        JPanel passwordPanel = new JPanel();
        JLabel passwordTitle = new JLabel("password");
        passwordPanel.add(passwordTitle);
        leftPanel.add(passwordPanel);

        JPanel fileDirectoryPanel = new JPanel();
        JLabel fileDirectoryTitle = new JLabel("fileDirectory");
        fileDirectoryPanel.add(fileDirectoryTitle);
        leftPanel.add(fileDirectoryPanel);

        JPanel commitMessagePanel = new JPanel();
        JLabel commitMessageTitle = new JLabel("commitMessage");
        commitMessagePanel.add(commitMessageTitle);
        leftPanel.add(commitMessagePanel);


    }

    private void initRight(){
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new GridLayout(5,1));
        this.add(rightPanel, BorderLayout.EAST);

        JPanel addrPanel = new JPanel();
        addrText = new JTextField(24);
        addrText.setText(gitUrl);
        addrPanel.add(addrText);
        rightPanel.add(addrPanel);

        JPanel usernamePanel = new JPanel();
        usernameText = new JTextField(24);
        usernameText.setText(username);
        usernamePanel.add(usernameText);
        rightPanel.add(usernamePanel);

        JPanel passwordPanel = new JPanel();
        passwordText = new JTextField(24);
        passwordText.setText(password);
        passwordPanel.add(passwordText);
        rightPanel.add(passwordPanel);

        JPanel fileDirectoryPanel = new JPanel();
        fileDirectoryText = new JTextField(24);
        fileDirectoryText.setText(localDir);
        fileDirectoryPanel.add(fileDirectoryText);
        rightPanel.add(fileDirectoryPanel);

        JPanel commitMessagePanel = new JPanel();
        commitMessageText = new JTextField(24);
        commitMessageText.setText(getTime() + "提交");
        commitMessagePanel.add(commitMessageText);
        rightPanel.add(commitMessagePanel);
    }

    private void initUnder(){
        JPanel underPanel = new JPanel();
        underPanel.setLayout(new GridLayout(2,1));
        this.add(underPanel, BorderLayout.SOUTH);

        JPanel uploadFlag = new JPanel();
        underPanel.add(uploadFlag);

        flagBox = new JCheckBox("自动上传到git",false);
        uploadFlag.add(flagBox);

        JPanel buttonPanel = new JPanel();
        JButton button1 = new JButton("确定");
        button1.addActionListener(e -> uploadToGit(1));
        button1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
        button1.setFont(new Font("宋体", Font.BOLD,12));
        button1.setForeground(Color.white);//字体颜色

        JButton button2 = new JButton("取消");
        button2.addActionListener(e -> closeDialog());
        button2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        button2.setFont(new Font("宋体", Font.BOLD,12));
        button2.setForeground(Color.white);//字体颜色
        buttonPanel.add(button1);
        buttonPanel.add(button2);

        underPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    public boolean uploadToGit(int tag){
        gitUrl = addrText.getText();
        username = usernameText.getText();
        password = passwordText.getText();
        localDir = fileDirectoryText.getText();

        if (StrUtil.isEmpty(gitUrl) || StrUtil.isEmpty(username) || StrUtil.isEmpty(password) || StrUtil.isEmpty(localDir)){
            if (tag == 1){
                JOptionPane.showMessageDialog(this.getComponent(0), "输入框不能为空！！！", "提示", JOptionPane.WARNING_MESSAGE);
            }else {
                if (StrUtil.isEmpty(gitUrl)){
                    SwingArea.tableModel.addRow(new Object[]{getTime(), "gitUrl为空，不能上传"});
                }
                if (StrUtil.isEmpty(username)){
                    SwingArea.tableModel.addRow(new Object[]{getTime(), "username为空，不能上传"});
                }
                if (StrUtil.isEmpty(password)){
                    SwingArea.tableModel.addRow(new Object[]{getTime(), "password为空，不能上传"});
                }
                if (StrUtil.isEmpty(localDir)){
                    SwingArea.tableModel.addRow(new Object[]{getTime(), "localDir为空，不能上传"});
                }
            }
            return false;
        }

        String startTime = DateUtil.format(new Date(), "MM-dd HH:mm:ss");
        SwingArea.tableModel.addRow(new Object[]{getTime(), "开始上传文件至git"});

        String commitMessage = "";
        if (tag == 1){
            this.dispose();
            // 按钮可点
            SwingArea.uploadAction.setEnabled(true);
            // 如果从表单传过来，则使用手动输入的值
            commitMessage = commitMessageText.getText();
        }else {
            // 如果基本自动调用，则使用生成的默认值
            commitMessage = startTime + "提交";
        }
        CredentialsProvider credential = GitUtils.createCredentialsProvider(username, password);
        Git git = null;
        try {
            git = GitUtils.getGit(gitUrl, credential, localDir);
            boolean commitFlag = GitUtils.commitFiles(git, credential, ".", commitMessage);
            SwingArea.tableModel.addRow(new Object[]{getTime(), "上传文件至git成功"});
            return commitFlag;
        } catch (IOException | GitAPIException e) {
            SwingArea.tableModel.addRow(new Object[]{getTime(), e.getMessage()});
            SwingArea.tableModel.addRow(new Object[]{getTime(), "上传文件失败"});
            return false;
        } finally {
            if (git != null){
                GitUtils.closeGit(git);
            }
        }
    }

    private void closeDialog(){
        this.dispose();
        SwingArea.uploadAction.setEnabled(true);
    }

    private String getTime(){
        return DateUtil.format(new Date(), "MM-dd HH:mm:ss");
    }
}